
<template>
  <div class="login">
    <el-form :model="formItem" :rules="loginRules" ref="formItem" label-width="80px" class="login-container">
      <div class="login-title">振大水泥数字化运营系统</div>
      <el-form-item label="账号" prop="userId" class="custom-label">
        <el-input v-model="formItem.userId" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" class="custom-label">
        <el-input type="password" v-model="formItem.password" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width:100%" type="primary" @click.native.prevent="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/auth'
import { refreshUser } from '@/utils'
export default {
  data () {
    return {
      formItem: {
        userId: '',
        password: ''
      },
      loginRules: {
        userId: [
          { required: true, message: '请输入员工号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    };
  },
  created () {
    localStorage.clear()
  },
  methods: {
    handleLogin () {
      this.$refs.formItem.validate(async (valid) => {
        if (valid) {
          const res = await login(this.formItem)
          if (res.code === 200) {
            setToken(res.data)
            refreshUser()
            this.$router.push('/')
          } else {
            this.$message.error(res.msg)
          }
        } else {
          console.log('提交失败!');
          return false;
        }
      });
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background-image: url('@/assets/img/loginBg.jpeg');
  background-repeat: no-repeat;
  background-size: cover;
  display: flex;
}
.login-container {
  width: 500px;
  margin: auto;
}
.login-title {
  text-align: center;
  margin: 0 auto 30px;
  color: #ffffff;
  font-size: 32px;
  font-weight: 500;
}
::v-deep(.custom-label .el-form-item__label) {
  color: #ffffff;
  font-size: 20px;
}
</style>